

import java.io.*;

public class MainClass {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		/*** test creating new corpus ***/
//		Corpus c = new Corpus("train.txt");
//		System.out.println("Number of unique words in corpus: " + c.X());
//		System.out.println("Number of total words in file: " + c.S());
		/*** end ***/
		
		/*** test splitting training file ***/
		System.out.println("Splitting train.txt");
		splitTrainingFile("train.txt", 0.9, "train90.txt", "train10.txt");
		System.out.println("Done splitting");
		/*** end ***/
		
		/*** test debugging corpus probabilities ***/
//		double sum = 0;
//		
//		for (String word : c.getUniqueWordSet())
//			sum += c.C(word);
//		
//		System.out.println("Sum of probabilites in corpus = " + sum / c.S());
		/*** end ***/
		
		/*** test creating new Lidstone ***/
//		Lidstone lidstone = new Lidstone(c);
//		lidstone.setLambda(0.1);
//		sum = 0;
//		for (String word : c.getUniqueWordSet())
//			sum += lidstone.getProbablity(word);
//		
//		System.out.println("Sum of probabilites in lidstone = " + sum);
		/*** end ***/
		
		/*** test debugging lidstone ***/
		Corpus c = new Corpus("train.txt");
		c.PRINT();
		System.out.println("Total S in train.txt = " + c.S());
		System.out.println("Total X in train.txt = " + c.X());
		
		Corpus c90 = new Corpus("train90.txt");
		Lidstone lidstone = new Lidstone(c90);
		System.out.println("Total S in train90.txt = " + c90.S());
		System.out.println("Total X in train90.txt = " + c90.X());
		lidstone.setLambda(0.05);
		
		System.out.println("Sum of probabilites in lidstone = " + lidstone.debugModel());
		/*** end ***/
		
		/*** test calculating MLE for lidstone ***/
		c = new Corpus("train90.txt");
		lidstone = new Lidstone(c);
		System.out.println("MLE is gained from Lambda = " + calcMaximumLikelihood(lidstone, "train10.txt"));
		/*** end ***/
	}
	
	public static double calcMaximumLikelihood(Lidstone lidstone, String trainFile) throws IOException{
		double maxValue = -Double.MAX_VALUE; 
		double maxLambda = 0;
		double curr;
		
		for (double lambda = 0.05; lambda <= 10; lambda += 0.05) {
			
			
			
			lidstone.setLambda(lambda);
			curr = lidstone.getLikelihood(trainFile);
			System.out.println(String.format("current lambda=%f max lambda=%f likelihood=%f", lambda, maxLambda, curr));
			if (curr > maxValue) {
				maxLambda = lambda;
				maxValue = curr;
			}
		}
		
		return maxLambda;
	}
	
	public static void splitTrainingFile(String source, double trainPercent, String out1, String out2) throws IOException {
		
		BufferedReader reader = new BufferedReader(new FileReader(source));
		String line = null;
		int wordCounter = 0;
		
		//Count how many words are in the file
		while ((line = reader.readLine()) != null) {
			String[] parsed = line.split(" ");
			wordCounter += parsed.length;
		}
		reader.close();
		
		int size = (int)(wordCounter * trainPercent);
		
		reader = new BufferedReader(new FileReader(source));
		line = null;
		wordCounter = 0;
		
		//Split words in file
		PrintWriter writer1 = new PrintWriter(out1);
		PrintWriter writer2 = new PrintWriter(out2);
		
		while ((line = reader.readLine()) != null) {
			String[] parsed = line.split(" ");
			
			for (String word : parsed) {
				if (wordCounter < size)
					writer1.println(word);
				else
					writer2.println(word);
				
				++wordCounter;
			}
		}
	}
}
